<?php namespace Common\Model;
use Hdphp\Model\Model;

class User extends Model{
	//指定表明
	protected $table = 'user';
	//框架验证
	protected $validate = array(
		array('email','email','必须为邮箱',3,3),
		array('email','required','邮箱不能为空',3,3),
		array('password','require','密码不能为空',3,3),
		array('password','minlen:6','密码不能少于6位',3,3)
		
	);
	//注册 自定义的store方法
	public function store(){
			//p($_SESSION['code']);
			//echo Code::get();
		//验证验证码是否正确
		if(strtolower($_SESSION['code']) != strtolower(Q('post.code'))){
			View::error('验证码不正确');
		}
		//验证用户名(邮箱)是否存在
		$email = Q('post.email');
		$userData = $this->where("email = '{$email}'")->find();
		if($userData){
			View::error('邮箱已被注册');
		}
		//验证两次密码是否正确
		if(Q('post.password') != Q('post.passwords')){
			View::error('两次密码不一致');
		}
		//1.调用框架里的自动验证方法，触发自动验证
		if(IS_POST){
			$_POST['password'] = Q('post.password','','md5');
			if($this->create()){
			//将密码加密后存入数据库				
			$this->add();
				return true;
			}
		}
		
	}
	//用户修改资料
	public function edit(){
		$Modeluser = new \Common\Model\User;
		//获得用户uid
	    $uid = $_SESSION['uid'];
		//组合用户提交的地址和生日
		$_POST['adres'] = Q('post.province') . ',' . Q('post.city') . ',' . Q('post.town') . ',' . Q('post.detail');
		$_POST['birth'] = Q('post.year') . ',' . Q('post.month') . ',' . Q('post.day');
		//把user表里没有的字段从$_POST里删除
		unset($_POST['year']);
		unset($_POST['month']);
		unset($_POST['day']);
		unset($_POST['province']);
		unset($_POST['city']);
		unset($_POST['town']);
		unset($_POST['detail']);
		$newUser = $_POST;
		$Modeluser->where("uid={$uid}")->save($newUser);
		return true;
	}
	
}










 ?>